package com.mo.ld.activity.interactive

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.drake.net.Get
import com.drake.net.Net
import com.drake.net.exception.NetUnknownHostException
import com.drake.net.exception.RequestParamsException
import com.drake.net.utils.scopeNetLife
import com.mo.ld.bean.ActionBean
import com.mo.lib.http.BannerBean
import com.mo.lib.http.HotBean
import com.mo.ld.modle.getMainAdapter2
import com.mo.lib.http.KCallBack
import com.mo.lib.ui.activity.BaseRvActivity
import com.mo.lib.utils.KLog
import com.mo.lib.utils.showToast
import okhttp3.Call

/**
 * @ author：mo
 * @ data：2022/4/26:9:33
 * @ 功能：协程
 */
class CoroutinesActivity : BaseRvActivity<ActionBean>() {
    override fun creatAdaper(): BaseQuickAdapter<ActionBean, BaseViewHolder> {
        return getMainAdapter2(mutableListOf(
            ActionBean("回调接口") {
                Net.get("https://www.wanandroid.com/banner/json") {}.enqueue(object : KCallBack<BannerBean>(this) {
                    override fun onSuccess(call: Call, result: BannerBean) {
                        showToast(result.data.toString())
                    }
                })
            },
            ActionBean("协程单调") {
                scopeNetLife {
//                    val get = Post<BannerBean>("appmobile/appv2/im/activity/findByCondationByApp") {
//                        param("page", "1")
//                        param("size", "2")
//                    }.await()
                    val get = Get<BannerBean>("https://www.wanandroid.com/banner/json").await()
                    showToast(get.toString())
                    KLog.i(get.toString())
                }
//                    .catch{
//                    KLog.i(it.toString())
//                }
            },
            ActionBean("协程并发") {
                scopeNetLife {
                    val get = Get<BannerBean>("https://www.wanandroid.com/banner/json")
                    val get2 = Get<HotBean>("https://www.wanandroid.com//hotkey/json")
                    val await = get.await()
                    val await1 = get2.await()
                    showToast("${await.data[0].desc}==${await1.data[0].name}")
                }
            },
            ActionBean("协程串行") {
                scopeNetLife {
                    val get = Get<BannerBean>("https://www.wanandroid.com/banner/json").await()
                    val get2 = Get<HotBean>("https://www.wanandroid.com//hotkey/json1").await()
                    showToast("${get.data[0].desc}==${get2.data[0].name}")
                }
            },
            ActionBean("协程单例捕获异常") {
                scopeNetLife {
                    val get = Get<BannerBean>("https://www.wanandroid.com/banner/json").await()
                    showToast("$get")
                }.catch {
                    handleError(it) //加载框架自带的异常处理，基本上是吐司
                    when (it) {
                        is NetUnknownHostException -> KLog.i("网络异常")
                        is RequestParamsException -> KLog.i("请求参数错误 400 - 499 范围内错误码")
                    }
                }.finally { //成功与否都会执行这里，成功it是空
                    it?.let {
                        KLog.i(it.toString())
                    }
                }
            },

            ))
    }

    override fun getList(page: Int) {
    }

    override fun getMore(page: Int) {
    }

}



